;Armory Viewer Updater
;Used to replace the older version with the new downloaded version

; Wait for Armory Viewer to close.
Process, WaitClose, ArmoryViewer.exe, 5
Process, WaitClose, Armory Viewer.exe, 5

; If Armory Viewer still didn't close, force it to
Process, Close, ArmoryViewer.exe
Process, Close, Armory Viewer.exe

; Check for old version
IfExist, Armory Viewer.exe
{
	FileDelete, Armory Viewer.exe
}

IfExist, installed
; The user installed it
{
	; Check if the installer exists
	IfNotExist, %A_ScriptDir%\aviewer.exe
	{
		MsgBox Exiting (aviewer not found)
		ExitApp
	}
	RunWait, %A_ScriptDir%\aviewer.exe /S /D=%A_WorkingDir%	; Install to current directory
	FileDelete, %A_ScriptDir%\aviewer.exe									; Delete installer
}
else
;The user extracted it
{
	;Check if 7-zip downloaded correctly
	IfNotExist, %A_ScriptDir%\7z.exe
	{
		MsgBox Exiting (7z not found)
		ExitApp
	}

	FileMove, aviewer.rar, aviewer.zip	
	RunWait, %A_ScriptDir%\7z.exe e -y %A_ScriptDir%\aviewer.zip,, Hide	; Extract the files
	FileDelete, %A_ScriptDir%\aviewer.zip											; Delete the archive
	FileDelete, %A_ScriptDir%\7z.exe												; Delete 7-zip
}

; Launch Armory Viewer when update is completed
; Compatibility with old version (until 1.1 is final)
IfExist, Armory Viewer.exe
{
	FileDelete, ArmoryViewer.exe ; If downgrading...
	FileDelete, Microsoft.WindowsAPICodePack.dll
	Run Armory Viewer.exe ; 1.0.x
}
else
{
	Run ArmoryViewer.exe ; 1.1.x
}


